package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.converter.AIReadConverter;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationDO;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationOptionDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationMapper;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationOptionMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocAISituationCommand;
import com.cskaoyan.wordmemorize.service.AIReadService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AIReadServiceImpl extends ServiceImpl<UserVocAISituationMapper, UserVocAISituationDO> implements AIReadService {


    @Resource
    UserVocAISituationMapper userVocAISituationMapper;

    @Resource
    UserVocAISituationOptionMapper userVocAISituationOptionMapper;

    @Resource
    AIReadConverter airReadConverter;


    @Override
    public void addAISituation(UserVocAISituationCommand command) {
        UserVocAISituationDO userVocAISituationDO = airReadConverter.userVocAISituationCommandToDO(command);

        UserVocAISituationDO userVocAISituationDOByDatabase = userVocAISituationMapper.selectOne(new QueryWrapper<UserVocAISituationDO>()
                .eq("name", userVocAISituationDO.getName())
        );

        if (userVocAISituationDOByDatabase != null) {
            throw new RuntimeException("模式名称已存在");
        } else {
            userVocAISituationMapper.insert(userVocAISituationDO);

            List<UserVocAISituationOptionDO> options = userVocAISituationDO.getOptions();
            for (UserVocAISituationOptionDO option : options) {
                option.setSituationId(userVocAISituationDO.getId());
                userVocAISituationOptionMapper.insert(option);
            }

        }

    }


    @Override
    @Transactional
    public void updateAISituation(UserVocAISituationCommand command) {

        UserVocAISituationDO userVocAISituationDO = airReadConverter.userVocAISituationCommandToDO(command);
        List<UserVocAISituationOptionDO> options = userVocAISituationDO.getOptions();

        // 直接修改数据库中user_voc_ai_situation表的数据
        userVocAISituationMapper.updateById(userVocAISituationDO);


        for (UserVocAISituationOptionDO option : options) {
            if (option.getId() == null) {
                if (option.getName() != null) {
                    // 添加situationId 并插入
                    option.setSituationId(userVocAISituationDO.getId());
                    userVocAISituationOptionMapper.insert(option);
                }
                // 否则是无效数据  不应该插入
            } else {
                // 存在  则进行修改
                userVocAISituationOptionMapper.updateById(option);
            }

        }


    }


    @Override
    @Transactional
    public void deleteAISituation(Long id) {

        userVocAISituationMapper.deleteById(id);
        LambdaUpdateWrapper<UserVocAISituationOptionDO> optionWrapper = new LambdaUpdateWrapper<>();
        optionWrapper.eq(UserVocAISituationOptionDO::getSituationId, id);
        userVocAISituationOptionMapper.delete(optionWrapper);
    }


    @Override
    public void deleteAISituationOption(Long id) {
        userVocAISituationOptionMapper.deleteById(id);
    }


    @Override
    @Transactional
    public void batchDeleteAISituationOption(List<Long> ids) {

        LambdaUpdateWrapper<UserVocAISituationDO> userVocAISituationDOWrapper = new LambdaUpdateWrapper<>();

        userVocAISituationDOWrapper.in(UserVocAISituationDO::getId, ids);

        // 整体删除situation表
        userVocAISituationMapper.delete(userVocAISituationDOWrapper);

        LambdaUpdateWrapper<UserVocAISituationOptionDO> optionDOLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 无差别删除  只要id在其中
        optionDOLambdaUpdateWrapper.in(UserVocAISituationOptionDO::getSituationId, ids);
        userVocAISituationOptionMapper.delete(optionDOLambdaUpdateWrapper);

    }


    @Override
    @Transactional
    public PageDTO<UserVocAISituationDTO> getAISituationList(PageRequest pageRequest) {


        pageRequest.setPageNum((pageRequest.getPageNum() - 1) * pageRequest.getCount());


        List<UserVocAISituationDO> records = userVocAISituationMapper.userVocAISituationDOList(pageRequest);

        Long total = userVocAISituationMapper.getTotal(pageRequest);
        return airReadConverter.toUserVocAISituationPage(records, total);


    }
}
